<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

class KomaMySQLDriver extends KomaDatabase implements IKomaDatabaseDriver
{
        function __construct($args = array())
        {
                if(!function_exists('mysql_connect')) throw new KomaLibraryException(array('code' => LIBRARY_NOT_INSTALLED, 'class' => __CLASS__, 'library' => 'MySQL'));
                
                $this->createProperties(array(
                              'Host' => isset($args['host']) ? $args['host'] : KOMA_DB_HOST,
                              'Username' => isset($args['username']) ? $args['username'] : KOMA_DB_USER,
                              'Password' => isset($args['password']) ? $args['password'] : KOMA_DB_PASS,
                              'Database' => isset($args['database']) ? $args['database'] : KOMA_DB_NAME,
                              'Persistent' => isset($args['persistent']) ? $args['persistent'] : KOMA_DB_PCONNECTION
                              ));

                $this->setReadOnlyProperties(array('Username', 'Password'));
                parent::__construct();
        }

        function __destruct()
        {
                $this->close();
        }
        
        function affectedRows()
        {
                return @mysql_affected_rows($this->Connection);
        }

	function close()
	{
                if(!$this->Persistent) @mysql_close($this->Connection);
	}
        
	function connect()
	{
                $this->Connection = ($this->Persistent ? @mysql_pconnect($this->Host, $this->Username, $this->Password) : @mysql_connect($this->Host, $this->Username, $this->Password));
                
                if(!is_resource($this->Connection))
                {
			switch(mysql_errno())
                        {
                                case 1045:
                                        throw new KomaDatabaseException(array('code' => UNABLE_TO_ACCESS,
                                                                              'user' => $this->Username,
                                                                              'database' => $this->Database)); break;
                                case 2005:
                                default:
                                        throw new KomaDatabaseException(array('code' => UNABLE_TO_CONNECT,
                                                                              'host' => $this->Host));
                        }
			
                        //throw new KomaDatabaseException(array('code' => UNABLE_TO_CONNECT,
                        //                                      'host' => $this->Host));
                }
                
                if(!@mysql_selectdb($this->Database, $this->Connection))
                {
                        switch(mysql_errno())
                        {
                                case 1044:
                                        throw new KomaDatabaseException(array('code' => UNABLE_TO_ACCESS,
                                                                              'user' => $this->Username,
                                                                              'database' => $this->Database)); break;
                                case 1049:
                                default:
                                        throw new KomaDatabaseException(array('code' => UNABLE_TO_USE,
                                                                              'database' => $this->Database));
                        }
                }
	}
	
	function fetchArray($result = null)
	{
                $this->assignResult($result);
		return @mysql_fetch_array($this->Result);
	}

        function fetchAssoc($result = null)
        {
                $this->assignResult($result);
                return @mysql_fetch_assoc($this->Result);
        }
        
	function fetchRow($result = null)
	{
                $this->assignResult($result);
		return @mysql_fetch_row($this->Result);
	}

        function fieldName($offset, $result  = null)
        {
                $this->assignResult($result);
                return @mysql_field_name($this->Result, $offset);
        }
	
        function numFields($result  = null)
        {
                $this->assignResult($result);
                return @mysql_num_fields($this->Result);
        }
	
	function numRows($result  = null)
        {
                $this->assignResult($result);
                return @mysql_num_rows($this->Result);
        }
        
	function query($query)
	{
                $this->Result = @mysql_query($query, $this->Connection);
                if($this->Result) return $this->Result;
                
                return false;
	}
        
	static function getInstance($args = array())
	{
                return self::createInstance(__CLASS__, $args);
	}
}
?>